local skel = fk.CreateSkill {
  name = "emo__youwu",
  tags = {Skill.Compulsory},
  --[[
  dynamic_desc = function (self, player, lang)
    local removed = player:getTableMark("emo__youwu_removed")
    local all = {"emo__youwu_empty", "emo__youwu_single", "emo__youwu_same"}
    if #removed == 3 then
      return "emo__youwu_none"
    elseif #removed > 0 then
      local arg1 = table.contains(removed, all[1]) and "" or all[1]
      local arg2 = table.contains(removed, all[2]) and "" or all[2]
      local arg3 = table.contains(removed, all[3]) and "" or all[3]
      return "emo__youwu_dyn:" .. arg1 .. ":" .. arg2 .. ":" .. arg3
    end
  end,
  --]]
}

Fk:loadTranslationTable{
  ["emo__youwu"] = "幽舞",
  [":emo__youwu"] = "锁定技，当你进入濒死状态时，你展示所有手牌，若其中：①无手牌，你摸四张牌；②均为一种颜色，你将所有手牌当【酒】使用；③有手牌且黑红张数相同，本回合防止你受到的伤害。若均不为以上，你重铸所有牌。",

  ["#emo__youwu-recast"] = "幽舞：重铸任意张牌",
  ["@@emo__youwu-turn"] = "幽舞",

  -- 废案
  [":emo__youwu_none"] = "锁定技，当你进入濒死状态时，你展示所有手牌，然后重铸任意张牌。",
  [":emo__youwu_dyn"] = "锁定技，当你进入濒死状态时，你展示所有手牌，若其中：{1}{2}{3}若均不为以上，你移除一项，然后重铸任意张牌。",

  ["emo__youwu_empty"] = "①无手牌，你摸四张牌；",
  ["emo__youwu_single"] = "②均为一种颜色，你将所有手牌当【酒】使用；",
  ["emo__youwu_same"] = "③有手牌且黑红数量相同，本回合防止你受到的伤害。",
}

skel:addEffect(fk.EnterDying, {
  anim_type = "defensive",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(skel.name)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    --local removed = player:getTableMark("emo__youwu_removed")
    local hand = player:getCardIds("h")
    local red, black = 0, 0
    if #hand > 0 then
      player:showCards(hand)
      if player.dead then return end
      room:delay(400)
      for _, id in ipairs(hand) do
        local color = Fk:getCardById(id).color
        if color == Card.Red then
          red = red + 1
        elseif color == Card.Black then
          black = black + 1
        end
      end
    end
    if #hand == 0 then
      player:drawCards(4, skel.name)
    elseif black == red and red > 0 then
      room:setPlayerMark(player, "@@emo__youwu-turn", 1)
    elseif black == #hand or red == #hand then
      local card = Fk:cloneCard("analeptic")
      card:addSubcards(player:getCardIds("h"))
      card.skillName = skel.name
      if player:canUseTo(card, player, {bypass_times = true}) then
        room:useCard{
          from = player, tos = {player}, card = card, extraUse  = true, extra_data = {analepticRecover = true}
        }
      end
    elseif not player:isNude() then
      --[[
      local cards = room:askToCards(player, {
        min_num = 1, max_num = 999, include_equip = true, cancelable = true, skill_name = skel.name,
        prompt = "#emo__youwu-recast",
      })
      --]]
      local cards = player:getCardIds("h")
      if #cards > 0 then
        room:recastCard(cards, player, skel.name)
      end
    end
  end,
})

skel:addEffect(fk.DamageInflicted, {
  anim_type = "defensive",
  is_delay_effect = true,
  priority = 1.01,
  can_trigger = function (self, event, target, player, data)
    return target == player and player:getMark("@@emo__youwu-turn") ~= 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    data:preventDamage()
  end,
})

return skel
